package ferp.android.activities;

import android.app.Dialog;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import ferp.android.R;
import ferp.android.ads.interstitial.InterstitialAdManager;
import ferp.android.dialogs.IntroductionDialog;
import ferp.android.managers.ProfileManager;
import ferp.core.card.Card;
import ferp.core.game.Game;
import ferp.core.game.Input;
import ferp.core.game.Settings;
import ferp.core.player.Player;
import ferp.core.player.Profile;
import ferp.core.tutorial.Scenario;

/**
 * User: olegs
 * Date: 11/10/2014 23:47
 */
public class Tutorial extends Main
{
  @Override
  public void onBackPressed() {showDialog(Dialogs.EXIT_CONFIRMATION_TUTORIAL);}

  @Override
  public boolean onCreateOptionsMenu(Menu menu)
  {
    super.onCreateOptionsMenu(menu);

    menu.findItem(R.id.main_pool).setVisible(false);
    menu.findItem(R.id.main_score).setVisible(false);
    menu.findItem(R.id.main_settings).setVisible(false);

    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item)
  {
    switch (item.getItemId())
    {
      case R.id.main_introduction:
        showDialog(Dialogs.INTRODUCTION);
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override
  public void onItemSelected(int requestCode, int selected)
  {
    Profile profile = profile();
    Game g = profile.game();

    switch (requestCode)
    {
      case Dialogs.EXIT_CONFIRMATION_TUTORIAL:
        switch (selected)
        {
          case ExitOptionTutorial.REPEAT:
            clean();
            profile.replayTutorial();

            Game.log(g, this, "TUTORIAL_REPEAT");
            process.now();

            break;

          case ExitOptionTutorial.EXIT:
            exit();

            break;
        }

        break;
    }
  }


  @Override
  protected Dialog onCreateDialog(int id)
  {
    switch (id)
    {
      case Dialogs.INTRODUCTION:
        return IntroductionDialog.create(this, this, profile(), id);

      default:
        return super.onCreateDialog(id);
    }
  }

  @Override
  protected void onPrepareDialog(int id, Dialog dialog)
  {
    switch (id)
    {
      case Dialogs.INTRODUCTION:
        ((IntroductionDialog)dialog).prepare();
        break;

      default:
        super.onPrepareDialog(id, dialog);
    }
  }

  @Override
  public void onCancel(int requestCode)
  {
    switch (requestCode)
    {
      case Dialogs.INTRODUCTION:
        Game g = game();

        removeDialog(Dialogs.INTRODUCTION);

        if (g.options.isSet(Input.Options.WAIT_FOR_INTRODUCTION))
        {
          g.options.clear();
          process.now();
        }
        break;

      default:
        super.onCancel(requestCode);
    }
  }

  @Override
  public void showIntroduction(Game g, Settings settings)
  {
    table.reset(g, settings, true);

    showDialog(Dialogs.INTRODUCTION);
  }

  @Override
  public void showNote(Scenario.Note note) {table.showNote(note);}

  @Override
  public void onFirstHumanBid(Game game, Player human) {/* nothing to do */}

  @Override
  public void onTrickingTenCheckStarted(Game g) {/* nothing to do */}
  @Override
  public void onTrickingTenCheckRequested(Game g) {/* nothing to do */}
  @Override
  public void onTrickingTenCheckFinished(Game g) {/* nothing to do */}

  @Override
  public void onFastMovingFinished(Game g) {/* nothing to do */}
  @Override
  public void onFastMovingStarted(Game g) {/* nothing to do */}
  @Override
  public void onFastMovingRequested(Game g) {/* nothing to do */}

  @Override
  public void onSetOver(Game g) {/* nothing to do */}

  @Override
  public void onGameOver(Game g)
  {
    startActivity(new Intent(getApplicationContext(), Entrance.class));
    InterstitialAdManager.show(this);
    finish();
  }

  @Override
  public void onResolverRestoreRequested(Game g, Settings settings)
  {
    onGameRestoreEnd(g);
  }

  @Override
  protected ProfileManager getProfileManager() {return ProfileManager.tutorial(this);}

  @Override
  protected boolean isResolverAvailable() {return true;}

  @Override
  public void onResolverInitialize(Game g, Settings settings) throws Game.Error {}

  @Override
  public void onRecalculateOnNextSolveRequested(int trick) throws Game.Error {/* nothing to do */}

  @Override
  protected boolean isResolving() {return false;}

  @Override
  protected int getBestScore() throws Game.Error {return 0;}

  @Override
  protected void resolve(Player mover) throws Game.Error {onTreeReady(mover);}

  @Override
  protected byte getBestMove(Game g, int depth) throws Game.Error
  {
    Scenario scenario = g.scenario();
    Card c = scenario.card(g.rounds, g.current());

    return c.compressed;
  }

  // exit tutorial dialog's options
  private static final class ExitOptionTutorial
  {
    private static final int REPEAT = 0;
    private static final int EXIT   = 1;
  }
}
